home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / motif / simplest.c.z / simplest.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  2.7 KB  |  101 lines

  1. /*
  2.  * simplest - simple single buffered RGBA motif program.
  3.  */
  4. /* compile: cc -o simplest simplest.c -lGLw -lGL -lXm -lXt -lX11 */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <Xm/Frame.h>
  8. #include <X11/GLw/GLwMDrawA.h>
  9. #include <X11/keysym.h>
  10. #include <X11/Xutil.h>
  11. #include <GL/glx.h>
  12.  
  13. static int attribs[] = { GLX_RGBA, GLX_RED_SIZE, 1, None};
  14.  
  15. static String fallbackResources[] = {
  16.     "*sgiMode:    True",
  17.     "*useSchemes: all",
  18.     "*glxwidget*width: 300", "*glxwidget*height: 300",
  19.     "*frame*shadowType: SHADOW_IN",
  20.     NULL};
  21.  
  22. void
  23. draw_scene(void) {
  24.    glClearColor(0.5, 0.5, 0.5, 1.0);
  25.    glClear(GL_COLOR_BUFFER_BIT);
  26.    glColor3f(1.0,0.0,0.0);
  27.    glRectf(-.5,-.5,.5,.5);
  28.    glColor3f(0.0,1.0,0.0);
  29.    glRectf(-.4,-.4,.4,.4);
  30.    glColor3f(0.0,0.0,1.0);
  31.    glRectf(-.3,-.3,.3,.3);
  32.    glFlush();
  33. }
  34.  
  35. static void
  36. input(Widget w, XtPointer client_data, XtPointer call) {
  37.    char buf[31];
  38.    KeySym keysym;
  39.    XEvent *event = ((GLwDrawingAreaCallbackStruct *) call)->event;
  40.  
  41.    switch(event->type) {
  42.    case KeyRelease:
  43.       XLookupString(&event->xkey, buf, sizeof buf, &keysym, NULL);
  44.       switch(keysym) {
  45.       case XK_Escape :
  46.      exit(EXIT_SUCCESS);
  47.      break;
  48.       default: break;
  49.       }
  50.       break;
  51.    }
  52. }
  53.  
  54. static void
  55. resize(Widget w, XtPointer client_data, XtPointer call) {
  56.    GLwDrawingAreaCallbackStruct *call_data;
  57.    call_data = (GLwDrawingAreaCallbackStruct *) call;
  58.  
  59.    glViewport(0, 0, call_data->width, call_data->height);
  60. }
  61.  
  62. static void
  63. expose(Widget w, XtPointer client_data, XtPointer call) {
  64.     GLwDrawingAreaCallbackStruct *call_data;
  65.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  66.  
  67.     draw_scene();
  68. }
  69.  
  70. main(int argc, char *argv[]) {
  71.     Display        *dpy;
  72.     XtAppContext    app;
  73.     XVisualInfo    *visinfo;
  74.     GLXContext      glxcontext;
  75.     Widget          toplevel, frame, glxwidget;
  76.  
  77.     toplevel = XtOpenApplication(&app, "simplest", NULL, 0, &argc, argv,
  78.         fallbackResources, applicationShellWidgetClass, NULL, 0);
  79.     dpy = XtDisplay(toplevel);
  80.  
  81.     frame = XmCreateFrame(toplevel, "frame", NULL, 0);
  82.     XtManageChild(frame);
  83.  
  84.     /* specify visual directly */
  85.     if (!(visinfo = glXChooseVisual(dpy, DefaultScreen(dpy), attribs)))
  86.     XtAppError(app, "no suitable RGB visual");
  87.  
  88.     glxwidget = XtVaCreateManagedWidget("glxwidget", glwMDrawingAreaWidgetClass,
  89.     frame, GLwNvisualInfo, visinfo, NULL);
  90.     XtAddCallback(glxwidget, GLwNexposeCallback, expose, NULL);
  91.     XtAddCallback(glxwidget, GLwNresizeCallback, resize, NULL);
  92.     XtAddCallback(glxwidget, GLwNinputCallback, input, NULL);
  93.  
  94.     XtRealizeWidget(toplevel);
  95.  
  96.     glxcontext = glXCreateContext(dpy, visinfo, 0, GL_TRUE);
  97.     GLwDrawingAreaMakeCurrent(glxwidget, glxcontext);
  98.  
  99.     XtAppMainLoop(app);
  100. }
  101.